import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';

import 'plugin_image_platform_interface.dart';

class MethodChannelPluginImage extends PluginImagePlatform {
  @visibleForTesting
  final methodChannel = const MethodChannel('plugin_image');
  final EventChannel _eventChannel = const EventChannel("plugin_camera_event");

  MethodChannelPluginImage() {
    deviceController = StreamController();
    deviceStream = deviceController.stream.asBroadcastStream();
    _eventChannel.receiveBroadcastStream().listen(_onEvent, onError: _onError);
  }

  void _onEvent(event) {
    Map map = event;
    if (kDebugMode) {
      debugPrint("--------------map:$map");
    }
    //添加监听
    deviceController.sink.add(map);
  }

  void _onError(Object error) {
    if (kDebugMode) {
      debugPrint("--------------error:$error");
    }
  }

  @override
  Future<String?> getPlatformVersion() async {
    final version = await methodChannel.invokeMethod<String>('getPlatformVersion');
    return version;
  }

  @override
  Future openCamera() async {
    final version = await methodChannel.invokeMethod<String>('openCamera');
    return version;
  }

  @override
  Future<Map?> openPhotoAlbum() async {
    final imageMap = await methodChannel.invokeMethod<Map>('openPhotoAlbum');
    return imageMap;
  }
}
